import axios from 'axios'
import cookie from '@/tools/cookie.js'
import md5 from '@/tools/md5.js'

export default
function routerPermission (router) {
  axios.interceptors.response.use(response => {
    let hasLogin=cookie.getCookie('hasLogin')
    if (hasLogin != md5(1)) {
     setTimeout(() => {
      router.replace({
        path: '/login'
      })
    }, 0)
     return response
   } else {
    return response
  }
}, error => {
  return Promise.reject(error)
})

  axios.interceptors.request.use(config => {
    axios.defaults.withCredentials=true;
    config.headers.withCredentials = true;
    axios.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
    return config;
  }, 
  error => {
    return Promise.reject(error)
  })


  router.beforeEach((to, form, next) => {
  	window.scrollTo(0, 0)
    let hasLogin=cookie.getCookie('hasLogin')
    let studentNumber=cookie.getCookie('studentNumber')
    if (hasLogin != md5(1)) {
      setTimeout(() => {
        router.replace({
          path: '/login'
        }, 0)
      })
      return next()
    }else{
      if(studentNumber==''){
        // setTimeout(() => {
        //   cookie.clearCookie()
        //   router.replace({
        //     path: '/login'
        //   }, 0)
        // })
        // return next()
      }
    }

    return next()
  })

}
